<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>部门管理</title>
<jsp:include page="../inc.jsp"></jsp:include>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/javascript/comboBoxTree.js"
	charset="utf-8"></script>
	<script type="text/javascript"
	src="${pageContext.request.contextPath}/javascript/selector/deptDialog.js"
	charset="utf-8"></script>
<script type="text/javascript">
	/**
	 * 企业管理
	 */

	/* ----------------------- private属性 ----------------------- */
	var gridEl = 'repTime-grid';
	var grid, form, selModel, toolbar;
	var win, treeEl = "deptTree";
	var searchFlds;
	var treeStore;
	var departmentName = "所有部门",  deptCode;
	/* ----------------------- private方法 ----------------------- */
	// 创建列表
	function createGrid() {
		var queryFlds = new Array();
		searchFlds = new Ext.util.MixedCollection();
		queryFlds.push('name');
		var fldTimeName = new Ext.form.TextField({
			name : 'name',
			id : gridEl + '-name',
			fieldLabel : '名称',
			allowBlank : true,
			width : 200
		});

		searchFlds.add('name', fldTimeName);
		grid = Ext.create('gridView', {
			remoteSort : true,
			gridId : gridEl,
			checkboxSelectionModel : true,
			queryFlds : queryFlds,
			searchFlds : searchFlds,
			entityClassName : 'com.rover.entity.system.Department',
			queryUrl : '../departmentController/query.action',
			deleteUrl : '../departmentController/delete.action',
			jsonId : 'id',
			recordPk : [ "id" ]
		});

		grid.getStore().on('beforeload', function(store, options) {
			store.proxy.extraParams.deptCode = deptCode;
		}, this, true);
	}

  var deptWin=null;
   function selectDept(){
	   if(!deptWin){
		   deptWin= new deptDialog();
		   deptWin.on('onSelectItem', function(deptCode, name) {
				form.getForm().findField('pDeptCode').setValue(deptCode);
				form.getForm().findField('pDeptName').setValue(name);
				}, this);
			}
	   deptWin.show();
   }

	// 刷新列表
	function reloadRecords() {
		grid.getStore().reload();
	}

	function createForm() {
		Ext.define('departmentForm',
				{
					extend : 'Ext.form.Panel',
					fields : new Ext.util.MixedCollection(),
					initComponent : function() {
						var me = this;
						var tn = 1;

						this.fields.add("id", new Ext.form.Hidden({
							name : "id",
							dataIndex : "id"
						}));
						this.fields.add("deptName", new Ext.form.TextField({
							name : "deptName",
							dataIndex : "deptName",
							fieldLabel : "部门名称",
							width :50,
							allowBlank : false
						}));

						this.fields.add("deptSn", new Ext.form.TextField({
							name : "deptSn",
							dataIndex : "deptSn",
							fieldLabel : "部门编码",
							width : 100,
							allowBlank : false
						}));
						this.fields.add("remark", new Ext.form.TextField({
							name : "remark",
							dataIndex : "remark",
							fieldLabel : "描述",
							width : 100,
							allowBlank : true
						}));
						this.items = [ {
							layout : "form",
							columnWidth : 1,
							labelAlign : "right",
							labelWidth : 100,
							items : [ {
								layout : "column",
								border : false,
								items : [{
														xtype : 'textfield',
														id : 'pDeptName',
														name : 'pDeptName',
														 readOnly:true,
														fieldLabel : '上级部门'
													},{
														xtype : 'hidden',
														name : 'pDeptCode',
															id : 'pDeptCode'
										}, new Ext.Button({
											id : "btn_queryDepart",
											icon : 'resources/img/fam.icons/search.gif',
											handler : function() {
												selectDept();
											}
										})]
							} ,this.fields.get("id"),
									this.fields.get("deptName"),
									this.fields.get("deptSn"),
									this.fields.get("remark") ]
						} ];
						this.frame = true;
						this.layout = 'fit';
						this.region = 'east';
						this.border = true;
						this.split = true;
						
						this.callParent(arguments);
					},
					onRender : function() {
						this.callParent(arguments);
					},
					newDataRecord : function() {
						this.fields.get("id").setValue('');
						this.fields.get("deptName").setValue('');
						this.fields.get("deptSn").setValue('');
						this.fields.get("remark").setValue('');
						Ext.getCmp("pDeptName").setValue('');
						Ext.getCmp("pDeptCode").setValue('');
					}

				});
	}

	// 修改的记录，初始化表单
	function EditRecord() {
		var sm = grid.getSelectionModel();
		var records = sm.getSelection();

		if (records.length <= 0) {
			Ext.Msg.alert('错误', "未选中记录，不能修改！");
			return;
		}
		var data = records[0].data;
		form.getForm().findField('id').setValue(data.id);
		form.getForm().findField('deptName').setValue(data.name);
		form.getForm().findField('deptSn').setValue(data.deptSn);
		form.getForm().findField('remark').setValue(data.remark);
		createWindows();
	}

	function createWindows() {
		if (win == null) {
			win = new Ext.Window(
					{
						title : '新增',
						iconCls : 'icon-win',
						autoHeight : true,
						width : 335,
						height : 200,
						buttonAlign : 'center',
						buttons : [
								{
									text : "保存",
									handler : function() {
										if (form.getForm().isValid()) {
											form.getForm().submit(
												{
													url : '../departmentController/save.action',
													scope : form,
													success : function(form,action) {
														win.hide();
														grid.getStore().reload();
														tree.store.reload();
													},
													failure : function(
															form,action) {
														if (action.result.message == undefined)
															Ext.Msg.alert('错误','网络连接失败.');
														else
															Ext.Msg.alert('错误',action.result.message);
													},
													waitMsg : '保存数据...'
												});
										} else {
											Ext.Msg.alert('提示', '表单含有无效或空数据');
										}
									},
									id : "btnSave"
								}, {
									text : "关闭",
									id : "btnClose",
									handler : function() {
										win.hide();
									}
								} ],
						layout : 'fit',
						plain : false,
						closable : false,
						modal : true,
						items : [ form ]
					});
		}
		win.show();

	}

	// 新建或修改表单成功提交后,重新加载其父节点
	function afterCommitFormSuccess(operation, recordData) {
		onWinHide();
		grid.getStore().reload();
	}
	// 创建用户--角色树
	function createTree() {
		if (!Ext.get(treeEl))
			Ext.DomHelper.append(document.body, {
				html : '<div id="' + treeEl + '"></div>'
			});

		treeStore = Ext.create('Ext.data.TreeStore', {
			proxy : {
				type : 'ajax',
				url : '../departmentController/getTreeJson.action'
			},
			sorters : [{
						property : 'leaf',
						direction : 'ASC'
					}, {
						property : 'text',
						direction : 'ASC'
					}],
			root : {
				id : "root-0",
				text : "所有部门",
				expanded : true
			}
		});
		tree = new Ext.tree.Panel({
			layout : 'fit',
			region : 'east',
			collapsible : true,
			collapsedTitle : true,
			border : true,
			split : false,
			width : 200,
			el : treeEl,
			animate : false,
			autoScroll : true,
			enableDD : false,
			containerScroll : true,
			lines : true,
			rootVisible : true,
			title : '部门',
			store:treeStore
		});
		if (Ext.isIE) {
			tree.animate = false;
			tree.animCollapse = false;
		}

		root = tree.root;
		tree.on({
	        'itemclick' : function(thisTree, record, item, index, e, options) {
	           //    thisTree自己本身
	            //record 点击选中的记录，rcd.get('id'); //节点id
	        	if (null != record.get('id') && "root-0" != record.get('id')) {
	    			deptCode = record.get('id');
	    			grid.getStore().load();
	    		}
	    		
	       }
	    });
	}
	
	// 创建layout
	function createLayout() {
		var filed1 = new Ext.form.Field();
		toolbar = new Ext.Toolbar({
			items : [ {
				text : "新增",
				iconCls : 'addIcon',
				id : "tlb_NEW",
				handler : function() {
					form.newDataRecord();
					createWindows();
				}
			}, '-', {
				text : "编辑",
				iconCls : 'editIcon',
				id : "tlb_EDIT",
				handler : function() {
					EditRecord();
				}
			}, '-', {
				iconCls : 'deleteIcon',
				text : "删除",
				id : "tlb_DELETE",
				handler : function() {
					grid.deleteRecord();
				}
			}, "->", searchFlds.get("name"), {
				text : "搜索",
				iconCls : 'searchIcon',
				handler : function() {
					grid.load();
				}
			} ]
		});
		
		var viewport = new Ext.Viewport({
			layout : 'border',
			items : [ {
				layout : 'border',
				region : 'west',
				border : false,
				width : 300,
				items : [ {
					layout : 'fit',
					region : 'center',
					border : false,
					items : [ tree ]
				} ]
			}, {
				layout : 'border',
				region : 'center',
				border : false,

				items : [ {
					layout : 'fit',
					region : 'north',
					border : false,
					items : [ toolbar ],
					height : 25
				}, {
					layout : 'fit',
					region : 'center',
					border : false,
					width : 400,
					items : [ grid ]
				} ]
			} ]
		});
		viewport.doLayout();
	}

	Ext.onReady(function() {
		Ext.QuickTips.init();
		createGrid();
		createForm();
		createTree();
		createLayout();

		form = Ext.create('departmentForm');
	});
</script>
</head>
<body>

</body>
</html>